﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.Services.DataContext;
using Economo.Services.EntitiesConversion;

namespace Economo.Services.BusinessLogic
{
    public class CreditCardsBusinessLogic
    {
        public List<BusinessEntities.CreditCard> GetCreditCards()
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                List<DataContext.CreditCard> cards = da.GetCreditCards();
                List<BusinessEntities.CreditCard> result = Convertions.ConvertToBusinessEntity<BusinessEntities.CreditCard, CreditCard>(cards);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }
        public List<BusinessEntities.CreditCard> GetCreditCards(byte bankId)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                List<DataContext.CreditCard> cards = da.GetCreditCards(bankId);
                List<BusinessEntities.CreditCard> result = Convertions.ConvertToBusinessEntity<BusinessEntities.CreditCard, CreditCard>(cards);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }

        /// <summary>
        /// Gets a credit card by BankId and card Id
        /// </summary>
        /// <param name="bankId"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public BusinessEntities.CreditCard GetCardById(byte bankId, byte cardId)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                DataContext.CreditCard card = da.GetCardById(bankId, cardId);
                BusinessEntities.CreditCard result = Convertions.ConvertToBusinessEntity<BusinessEntities.CreditCard>(card);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }
        /// <summary>
        /// Gets a card  by card Id
        /// </summary>        
        /// <param name="accountId"></param>
        /// <returns></returns>
        public BusinessEntities.CreditCard GetCardById(byte cardId)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                DataContext.CreditCard card = da.GetCardById(cardId);
                BusinessEntities.CreditCard result = Convertions.ConvertToBusinessEntity<BusinessEntities.CreditCard>(card);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }
        }

        public byte Insert(BusinessEntities.CreditCard newAccount)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                CreditCard newCard = Convertions.ConvertToDataEntity<CreditCard>(newAccount);
                newCard.Id = da.Insert(newCard);
                return newCard.Id;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return 0;
            }
            finally
            {
                da.Dispose();
            }
        }

        public void Update(BusinessEntities.CreditCard updatedCard)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                CreditCard card = Convertions.ConvertToDataEntity<CreditCard>(updatedCard);
                da.Update(card);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }
        }

        public void Delete(BusinessEntities.CreditCard cardToDelete)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                CreditCard card = Convertions.ConvertToDataEntity<CreditCard>(cardToDelete);
                da.Delete(card);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }
        public void Delete(byte bankId, byte cardId)
        {
            CreditCardDataContext da = new CreditCardDataContext();
            try
            {
                da.Delete(bankId, cardId);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }

    }
}
